﻿using System;
using System.IO;
using System.Security.Cryptography; 

/// <summary>
/// Summary description for MySecurityManager
/// </summary>
public class MySecurityManager
{
	public MySecurityManager()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string Encrypt(string data)
    {
        MemoryStream Ms = new MemoryStream();
        Rfc2898DeriveBytes PasswordKey;
        SymmetricAlgorithm Algo;
        CryptoStream CrStream;

        string Password = "YourPassword";
        string SaltString = "YourSalt";

        Algo = new RijndaelManaged();
        PasswordKey = new Rfc2898DeriveBytes(Password, System.Text.Encoding.ASCII.GetBytes(SaltString));
        Algo.Key = PasswordKey.GetBytes(Algo.KeySize / 8);
        Algo.IV = PasswordKey.GetBytes(Algo.BlockSize / 8);

        ICryptoTransform EnCryptor = Algo.CreateEncryptor();
        Ms = new MemoryStream();
        CrStream = new CryptoStream(Ms, EnCryptor, CryptoStreamMode.Write);
        byte[] Data = System.Text.Encoding.ASCII.GetBytes(data);
        CrStream.Write(Data, 0, Data.Length);
        return Convert.ToBase64String(Ms.ToArray());
    }


    public static string Decrypt(string data)
    {
        MemoryStream Ms = new MemoryStream(Convert.FromBase64String(data));
        Rfc2898DeriveBytes PasswordKey;
        SymmetricAlgorithm Algo;
        CryptoStream CrStream;

        string Password = "YourPassword";
        string SaltString = "YourSalt";
        Algo = new RijndaelManaged();
        PasswordKey = new Rfc2898DeriveBytes(Password, System.Text.Encoding.ASCII.GetBytes(SaltString));
        Algo.Key = PasswordKey.GetBytes(Algo.KeySize / 8);
        Algo.IV = PasswordKey.GetBytes(Algo.BlockSize / 8);

        ICryptoTransform Decryptor = Algo.CreateDecryptor();
        CrStream = new CryptoStream(Ms, Decryptor, CryptoStreamMode.Read);
        StreamReader Sr = new StreamReader(CrStream);
        return Sr.ReadToEnd();
    }
}
